package com.itbour.web.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.dto.com.UserInfoDto;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
 * copyright pirate alliance team 2014
 * 用户登录校验拦截器 
 * @author songyong
 */

public class LoginStatusInterceptor extends AbstractInterceptor {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// TODO Auto-generated method stub

		String namespace=invocation.getProxy().getNamespace();
		String actionName=invocation.getProxy().getActionName();
		Map<String, Object> session=ActionContext.getContext().getSession();
		
//		if("/webgoods".equals(namespace)&&"homePageInit".equals(actionName)){
//			if(session.get("oldURL")!=null){
//				session.remove("oldURL");
//			}	
//		}
		
		if(("/webcom".equals(namespace)&&"init".equals(actionName))||("/webcom".equals(namespace)&&"UserLogin".equals(actionName))
				||("/webcom".equals(namespace)&&"registerUser".equals(actionName)))
		{
			
			return invocation.invoke();
		}
		else{
			UserInfoDto userDto=(UserInfoDto)session.get("user");
			if(userDto!=null){				
			return invocation.invoke();	
			}
			
            HttpServletRequest req = ServletActionContext.getRequest();  
            String path = req.getRequestURI();
            String queryString = req.getQueryString();
            if (queryString == null) {
                queryString = "";
            }
            String realPath = path + "?" + queryString;
            String realPath1=realPath.substring(1);
            session.put("oldURL", realPath1);
            return "login";
		}
	
}
}